{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-----\n",
    "* 文档内容：API文档阅读及计算机视觉入门（认知服务）\n",
    "* 记录时间：22春_API_AI_ML_week03\n",
    "* 电子笔记撰写者：魏宏奋\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上周学习内容\n",
    "\n",
    "### python requests 模块\n",
    "\n",
    "> 1. content作为响应内容\n",
    "    * 数据样态？\n",
    "> 2. text检查相应内容\n",
    "    * 观察数据？\n",
    "> 3. 观察payload的结果\n",
    "\n",
    "### 实践 face++ 人脸检测/face detect\n",
    "\n",
    "* API实践步骤\n",
    "> 1. 导入需要的requests模块\n",
    "> 2. 输入我们需要API网站注册的API_Key\n",
    "> 3. 目标url [base url]\n",
    "> 4. 沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "> 5. 准备symbol ? 后面的数据,这里需要注意,一定要详细阅读API文档中的 “参数功能”,按照要求格式准备payload参数功能可能有:\n",
    ">> 1. 是否必要?必要的一定要准备好\n",
    ">> 2. 选填的一定是功能,要根据功能需求 好好填噢\n",
    "> 6. response响应。注意:\n",
    ">> 1. 详细阅读文档,注意请求方式(GET、POST、DELETE)\n",
    ">> 2. 注意json 和字典的差异 ,str vs dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 本周学习内容\n",
    "### Python 使用API模板\n",
    "* 建立自己的通用API模板，方便于重复调用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"H2XlOZDKIylh0a6nhQfyWQKUcCPkOPWo\"\n",
    "API_Secret = \"Oo22cBNY9YwAIDtBqDtPHJtDUBQQuOsY\"\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1654781201,8f20c775-fbae-40fb-bffd-08b2a1cb7766\",\"time_used\":419,\"faces\":[{\"face_token\":\"c725b850a1c4213c6d96d48a1baa4be7\",\"face_rectangle\":{\"top\":108,\"left\":141,\"width\":107,\"height\":107}}],\"image_id\":\"JoYdaZ0h1hGb4ih1d2EZVw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c725b850a1c4213c6d96d48a1baa4be7'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c725b850a1c4213c6d96d48a1baa4be7'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 人脸分析API\n",
    "* 使用上述模板合理调用参数来实现人脸分析API\n",
    "* 要求：获取 return_attributes 的所有参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "API_Key = \"H2XlOZDKIylh0a6nhQfyWQKUcCPkOPWo\"\n",
    "API_Secret = \"Oo22cBNY9YwAIDtBqDtPHJtDUBQQuOsY\"\n",
    "\n",
    "base_url2= \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\": \"c725b850a1c4213c6d96d48a1baa4be7\",\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.jrwenku.com%2Fwp-content%2Fuploads%2F2018%2F11%2F8-1542971093.jpeg&refer=http%3A%2F%2Fwww.jrwenku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649296881&t=479dc8c3a12f96ff2ac0e6c3725b358a\",\n",
    "    \"return_landmark\":1,\n",
    "    \"return_attributes\":'gender,age,smiling,glass,headpose,blur,eyestatus,emotion,facequality,beauty,mouthstatus,eyegaze,skinstatus'\n",
    "}\n",
    "\n",
    "p = requests.post(base_url2,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 200,\n",
       " 'request_id': '1654781585,750ef92b-1e40-4062-8997-4eb059b12efc',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 174, 'x': 180},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 176, 'x': 183},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 185, 'x': 206},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 174, 'x': 190},\n",
       "    'left_eye_upper_left_quarter': {'y': 128, 'x': 165},\n",
       "    'left_eyebrow_lower_middle': {'y': 119, 'x': 169},\n",
       "    'contour_chin': {'y': 214, 'x': 197},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 119, 'x': 161},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 117, 'x': 211},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 180, 'x': 208},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 182, 'x': 178},\n",
       "    'left_eye_bottom': {'y': 132, 'x': 170},\n",
       "    'mouth_lower_lip_bottom': {'y': 188, 'x': 196},\n",
       "    'contour_left9': {'y': 212, 'x': 185},\n",
       "    'mouth_lower_lip_top': {'y': 183, 'x': 195},\n",
       "    'right_eyebrow_upper_middle': {'y': 110, 'x': 218},\n",
       "    'right_eyebrow_left_corner': {'y': 117, 'x': 203},\n",
       "    'right_eye_bottom': {'y': 130, 'x': 218},\n",
       "    'contour_left7': {'y': 199, 'x': 165},\n",
       "    'contour_left6': {'y': 190, 'x': 157},\n",
       "    'contour_left5': {'y': 179, 'x': 151},\n",
       "    'contour_left4': {'y': 168, 'x': 146},\n",
       "    'contour_left3': {'y': 156, 'x': 143},\n",
       "    'contour_left2': {'y': 145, 'x': 142},\n",
       "    'contour_left1': {'y': 133, 'x': 140},\n",
       "    'left_eye_lower_left_quarter': {'y': 132, 'x': 165},\n",
       "    'mouth_upper_lip_top': {'y': 174, 'x': 194},\n",
       "    'contour_right3': {'y': 151, 'x': 246},\n",
       "    'contour_right2': {'y': 139, 'x': 246},\n",
       "    'mouth_left_corner': {'y': 175, 'x': 172},\n",
       "    'contour_right4': {'y': 163, 'x': 244},\n",
       "    'contour_right7': {'y': 195, 'x': 228},\n",
       "    'left_eyebrow_left_corner': {'y': 120, 'x': 154},\n",
       "    'nose_right': {'y': 157, 'x': 210},\n",
       "    'right_eye_upper_right_quarter': {'y': 125, 'x': 223},\n",
       "    'nose_tip': {'y': 160, 'x': 194},\n",
       "    'contour_right5': {'y': 175, 'x': 240},\n",
       "    'nose_contour_lower_middle': {'y': 166, 'x': 195},\n",
       "    'right_eye_top': {'y': 125, 'x': 217},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 187, 'x': 185},\n",
       "    'right_eye_right_corner': {'y': 127, 'x': 227},\n",
       "    'right_eye_lower_right_quarter': {'y': 129, 'x': 223},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 172, 'x': 209},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 116, 'x': 226},\n",
       "    'left_eye_left_corner': {'y': 130, 'x': 161},\n",
       "    'mouth_right_corner': {'y': 172, 'x': 218},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 174, 'x': 206},\n",
       "    'right_eye_lower_left_quarter': {'y': 130, 'x': 213},\n",
       "    'left_eyebrow_right_corner': {'y': 119, 'x': 185},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 119, 'x': 177},\n",
       "    'right_eye_center': {'y': 128, 'x': 218},\n",
       "    'left_eye_upper_right_quarter': {'y': 128, 'x': 176},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 181, 'x': 183},\n",
       "    'contour_left8': {'y': 206, 'x': 174},\n",
       "    'nose_left': {'y': 158, 'x': 179},\n",
       "    'right_eyebrow_lower_middle': {'y': 116, 'x': 218},\n",
       "    'left_eye_top': {'y': 127, 'x': 171},\n",
       "    'left_eye_center': {'y': 130, 'x': 170},\n",
       "    'left_eye_lower_right_quarter': {'y': 132, 'x': 175},\n",
       "    'nose_contour_right1': {'y': 130, 'x': 202},\n",
       "    'contour_right9': {'y': 211, 'x': 210},\n",
       "    'right_eye_left_corner': {'y': 130, 'x': 209},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 114, 'x': 160},\n",
       "    'left_eye_pupil': {'y': 129, 'x': 170},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 112, 'x': 210},\n",
       "    'contour_right8': {'y': 204, 'x': 220},\n",
       "    'right_eyebrow_right_corner': {'y': 116, 'x': 233},\n",
       "    'right_eye_upper_left_quarter': {'y': 126, 'x': 212},\n",
       "    'left_eyebrow_upper_middle': {'y': 113, 'x': 169},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 111, 'x': 226},\n",
       "    'nose_contour_left1': {'y': 130, 'x': 186},\n",
       "    'nose_contour_left2': {'y': 150, 'x': 182},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 173, 'x': 199},\n",
       "    'contour_right1': {'y': 128, 'x': 246},\n",
       "    'nose_contour_right2': {'y': 149, 'x': 206},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 180, 'x': 213},\n",
       "    'contour_right6': {'y': 185, 'x': 235},\n",
       "    'nose_contour_right3': {'y': 162, 'x': 203},\n",
       "    'nose_contour_left3': {'y': 162, 'x': 186},\n",
       "    'left_eye_right_corner': {'y': 131, 'x': 180},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 114, 'x': 177},\n",
       "    'right_eye_pupil': {'y': 127, 'x': 218},\n",
       "    'mouth_upper_lip_bottom': {'y': 176, 'x': 195}},\n",
       "   'attributes': {'emotion': {'sadness': 0.039,\n",
       "     'neutral': 0.001,\n",
       "     'disgust': 0.191,\n",
       "     'anger': 0.001,\n",
       "     'surprise': 0.031,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 99.736},\n",
       "    'beauty': {'female_score': 58.555, 'male_score': 58.212},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'mouthstatus': {'close': 0.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 100.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 1.554,\n",
       "     'stain': 9.17,\n",
       "     'acne': 1.527,\n",
       "     'health': 11.352},\n",
       "    'headpose': {'yaw_angle': 2.7483263,\n",
       "     'pitch_angle': 14.900063,\n",
       "     'roll_angle': 0.5866398},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.376},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.376},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.376}},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.005,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.995,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.003,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.001,\n",
       "      'no_glass_eye_open': 99.996,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 85.357},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.494,\n",
       "      'vector_z_component': 0.935,\n",
       "      'vector_x_component': -0.068,\n",
       "      'vector_y_component': 0.348,\n",
       "      'position_y_coordinate': 0.438},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.502,\n",
       "      'vector_z_component': 0.942,\n",
       "      'vector_x_component': 0.247,\n",
       "      'vector_y_component': 0.227,\n",
       "      'position_y_coordinate': 0.422}}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': 'c725b850a1c4213c6d96d48a1baa4be7'}]}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Tip：数据处理小魔法 pandas.json_normalize() 实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>c725b850a1c4213c6d96d48a1baa4be7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>landmark.mouth_upper_lip_left_contour2.y</th>\n",
       "      <td>174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>landmark.mouth_upper_lip_left_contour2.x</th>\n",
       "      <td>180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>landmark.mouth_upper_lip_left_contour3.y</th>\n",
       "      <td>176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>landmark.mouth_upper_lip_left_contour3.x</th>\n",
       "      <td>183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>226 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          c725b850a1c4213c6d96d48a1baa4be7\n",
       "landmark.mouth_upper_lip_left_contour2.y                                         174\n",
       "landmark.mouth_upper_lip_left_contour2.x                                         180\n",
       "landmark.mouth_upper_lip_left_contour3.y                                         176\n",
       "landmark.mouth_upper_lip_left_contour3.x                                         183\n",
       "...                                                                              ...\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.422\n",
       "face_rectangle.width                                                             107\n",
       "face_rectangle.top                                                               108\n",
       "face_rectangle.left                                                              141\n",
       "face_rectangle.height                                                            107\n",
       "\n",
       "[226 rows x 1 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(p.json()[\"faces\"]).T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 人脸对比API\n",
    "* 思考：人脸对比需要至少几张脸？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 人脸检测获取toekn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "API_Key = \"H2XlOZDKIylh0a6nhQfyWQKUcCPkOPWo\"\n",
    "API_Secret = \"Oo22cBNY9YwAIDtBqDtPHJtDUBQQuOsY\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "}\n",
    "\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'315378d0960181d2d00f9d9de8d6badf'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "API_Key = \"H2XlOZDKIylh0a6nhQfyWQKUcCPkOPWo\"\n",
    "API_Secret = \"Oo22cBNY9YwAIDtBqDtPHJtDUBQQuOsY\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "}\n",
    "\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'016fa05088d7b284e9581aa87f5bde5e'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 人脸对比环节"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "API_Key = \"H2XlOZDKIylh0a6nhQfyWQKUcCPkOPWo\"\n",
    "API_Secret = \"Oo22cBNY9YwAIDtBqDtPHJtDUBQQuOsY\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02\n",
    "}\n",
    "\n",
    "r = requests.post(url = compare_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1654782577,2ccf5db2-7aa6-4bbe-bd1e-06d1d178815a',\n",
       " 'time_used': 277,\n",
       " 'confidence': 83.194,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **疑问:** 每次进行一个api调用，就要重输次代码，能不能有什么方法能快速调用？\n",
    "### 封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "API_Key = \"H2XlOZDKIylh0a6nhQfyWQKUcCPkOPWo\"\n",
    "API_Secret = \"Oo22cBNY9YwAIDtBqDtPHJtDUBQQuOsY\"\n",
    "img_url_01 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 函数调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1654782822,70670e61-215a-432e-be13-1faca9f9cb48',\n",
       " 'time_used': 768,\n",
       " 'faces': [{'face_token': 'caa52c26dbbb0a6ce3b27c9b541a9f91',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1654782825,1b18fe31-f9b0-48b0-96f2-71c8cb9b211a',\n",
       " 'time_used': 436,\n",
       " 'faces': [{'face_token': 'ec82a3d878276d3bf81cd18b64fd65f3',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
